<?php

class Form {

	public static function getRequestedPath() { return Url::createCurrent()->getPath(); }

	public static function getUserIp() {

	    if (isset($_SERVER['REMOTE_ADDR']))
            return $_SERVER['REMOTE_ADDR'];

        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            return  $_SERVER['HTTP_X_FORWARDED_FOR'];

        return '0.0.0.0';
	}

    public static function getString($paramName, $minLength = null, $maxLength = null) {
        self::checkExists($paramName);
        return self::getStringAnyway($paramName, $minLength, $maxLength);
    }

    public static function getInt($paramName, $min = null, $max = null) {
        self::checkExists($paramName);
        return self::getIntAnyway($paramName, $min, $max);
    }

    public static function getPositiveInt($paramName) {
        return self::getInt($paramName, 0);
    }

    public static function getIntAnyway($paramName, $min = null, $max = null) {

    	if (isset($_GET[$paramName])) {
	        $val = (int) $_GET[$paramName];
    	} else if (isset($_POST[$paramName])) {
	        $val = (int) $_POST[$paramName];
    	} else {
    		$val = 0;
    	}

        if ($min !== null && $val < $min)
            throw new RequestException("'$paramName' too short");

        if ($max !== null && $val > $max)
            throw new RequestException("'$paramName' too long");

        return $val;
    }

    public static function bulkGetStringsAnyway(/* params */) {
        $arrParams = func_get_args();
        $arrResult = array();
        foreach ($arrParams as $paramName) {
            $arrResult[] = self::getStringAnyway($paramName);
        }
        return $arrResult;
    }


    public static function getPossibleParamAnyway($paramName, array $arrPossibleValues) {

        if (!$arrPossibleValues)
            throw new Exception('must be > 1 value for getParamAnyway');

        $param = self::getStringAnyway($paramName);

        return in_array($param, $arrPossibleValues) ?
            $param :
            reset($arrPossibleValues);
    }

    public static function isExists($paramName) {
        return isset($_GET[$paramName]) || isset($_POST[$paramName]);
    }

    public static function getStringAnyway($paramName, $minLength = null, $maxLength = null) {

    	if (isset($_GET[$paramName])) {
	        $val = (string) $_GET[$paramName];
    	} else if (isset($_POST[$paramName])) {
	        $val = (string) $_POST[$paramName];
    	} else {
    		$val = '';
    	}

        if ($minLength !== null && strlen($val) < $minLength)
            throw new RequestException("'$paramName' too short");

        if ($maxLength !== null && strlen($val) > $maxLength)
            throw new RequestException("'$paramName' too long");

        return trim($val);
    }


    private static function checkExists($paramName) {
        if (!isset($_GET[$paramName]) && !isset($_POST[$paramName]))
            throw new RequestException("Can not get '$paramName' from request");
    }
}

class RequestException extends Exception { }
